from django.http import HttpResponseBadRequest, HttpResponse, HttpRequest, JsonResponse
import hashlib
from .models import NewsClass, NewsSeo, NewsPower
from django.db.models import Q
from django.core.paginator import Paginator
import simplejson
import django.utils.timezone as timezone


def newspower(request):
	# 添加管理用户
	if request.method == 'POST':
		# request.params = request.GET
		return newspower_add(request)
	# 更新用户信息
	elif request.method == 'PUT':
		return newspower_edit(request)
	# 删除管理用户： 逻辑删除
	elif request.method == 'DELETE':
		return newspower_delete(request)

	else:
		return JsonResponse({"state": 0, "msg": "信息传递有问题"})


def newspower_add(request):
	# return HttpResponse('aaaaadsgbsvb')
	info = request.POST
	projects = info['projects']
	projects_id = info['projects_id']
	created_time = timezone.now()
	updatad_time = timezone.now()
	print(projects_id, projects)
	try:
		print("-----" * 5)
		item = NewsPower.objects.filter(Q(projects=projects) & Q(projects_id=projects_id))
		print("//////" * 100)
		print(item, item.query)
		if item:
			return JsonResponse({"state": 0, "msg": info['projects'] + "已存在,不能重复添加"})
		else:
			print("--------" * 100)

			if info['state']:
				state = info['state']
			else:
				state = 1

			print("+++++++" * 100)
			try:
				qs = NewsPower.objects.create(projects=info['projects'], projects_id=info['projects_id'], projects_content=info['projects_content'], group_id=info['group_id'], created_by=info['created_by'],created_time=created_time, updatad_by=info['updatad_by'], updatad_time=updatad_time)
				# info.save()
				# return JsonResponse('', safe=False)
				return JsonResponse({"state": 1, "msg": qs.title + "添加成功"})

			except NewsPower.DoesNotExist:
				return JsonResponse({"state": 0, "msg": "不能添加"})

	except NewsPower.DoesNotExist:
		return JsonResponse({"state": 0, "msg": "信息错误"})


def newspower_edit(request):
	# info = request.body
	info = simplejson.loads(request.body)
	# print(info['id'], info["newdata"]["username"])
	ids = info['id']
	newsdata = info["newdata"]
	try:
		customer = NewsPower.objects.get(pk=ids)
	except NewsPower.DoesNotExist:
		return JsonResponse({"state": 0, "msg": "信息存在,不能重复添加"})
	try:
		if 'projects' in newsdata:
			customer.projects = newsdata['projects']
		if 'projects_content' in newsdata:
			customer.projects_content = newsdata['projects_content']
		if 'description' in newsdata:
			customer.description = newsdata['description']
		if 'updatad_by' in newsdata:
			customer.updatad_by = newsdata['updatad_by']
		customer.update_time = timezone.now()
		customer.save()
		return JsonResponse({"state": 1, "msg": "修改成功"})
	except NewsPower.DoesNotExist:
		return JsonResponse({"state": 0, "msg": "修改出错"})


def newspower_delete(request):
	info = simplejson.loads(request.body)
	if len(info) == 1:
		ids = info['id']
		stateid = 3
	else:
		ids = info['id']
		stateid = info['status']

	try:
		delete_user = NewsPower.objects.get(pk=ids)
	# print(delete_user)
	except NewsPower.DoesNotExist:
		return JsonResponse({"state": 0, "msg": "用户不存在"})

	if stateid == 1 or stateid == 0:
		delete_user.state = stateid
		if stateid == 1:
			delete_user.delete_time is None
		delete_user.save()
		return JsonResponse({"state": 1, "msg": "修改成功"})
	else:

		if delete_user.delete_time is not None:
			return JsonResponse({"state": 0, "msg": "用户已经删除"})
		else:
			# customer.delete()
			delete_user.state = 0
			# 采用逻辑删除
			delete_user.delete_time = timezone.now()
			delete_user.save()
			return JsonResponse({"state": 1, "msg": "删除成功"})